package manager
{
	import data.BulletProp;
	import data.EBulletProp;
	import data.FlyProp;
	import data.Globals;
	import data.MonsterProp;
	import data.PlantProp;
	import data.TankProp;
	import data.WaveProp;
	import data.YaLuJiProp;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	public class MapConfig
	{
		/**
		 *路点 
		 */
		public var line:Array=[];
		/**
		 *障碍点 
		 */		
		public var barrier:Array = [];
		public var commonBarrier:Array = [];
		
		private static var instance:MapConfig;
		
		private var xml:URLLoader;
		
		private var request:URLRequest;
		
		public var loaderComplete:Function;
		public function MapConfig()
		{
			if(instance) throw new Error("This is singelon!") ;
			instance = this;
		}
		
		public static function getInstance():MapConfig{
			return instance ||=new MapConfig();
		}
		
		public function loadXml(url:String):void
		{
			xml = new URLLoader();
			request = new URLRequest("resources/xml/"+url+".xml"+"?="+Math.random()*10000)
			xml.load(request);
			xml.addEventListener(Event.COMPLETE,complete);
			xml.addEventListener(IOErrorEvent.IO_ERROR,io_error);
		}
		
		protected function io_error(event:IOErrorEvent):void
		{
			destroyEvent();
		}
		
		private function destroyEvent():void
		{
			xml.removeEventListener(Event.COMPLETE,complete);
			xml.removeEventListener(IOErrorEvent.IO_ERROR,io_error);
			xml = null;
			request = null;
		}
		
		public function complete(event:Event=null):void
		{
			trace(request.url);
			var xml:XML = XML(event.target.data);
			if(xml.hasOwnProperty("version")){
				Globals.version = xml.version;
			}
			
			if(xml.hasOwnProperty("totalLevel")){
				Globals.totalLevel = xml.totalLevel;
			}
			
			if(xml.hasOwnProperty("initialEnergy")){
				Globals.initialEnergy = xml.initialEnergy;
			}
			
			if(xml.hasOwnProperty("yaluji")){
				var yaluji:YaLuJiProp = new YaLuJiProp();
				yaluji.name = xml.yaluji.@name;
				yaluji.des = xml.yaluji.@des;
				yaluji.revive = xml.yaluji.@revive;
				yaluji.operation = xml.yaluji.@operation;
				yaluji.nameDes = xml.yaluji.@nameDes;
				ObjDataManage.getInstance().tankMap[yaluji.name] = yaluji;
			}
			
			var xmlList:XMLList;
			var xxx:XML;
			var point:Point;
			var i:int=0;
			var lens:int;
			//解析植物类型血量和能量值
			if(xml.hasOwnProperty("plantsInfo")){
				xmlList = xml.plantsInfo.children();
				for each(xxx in xmlList){
					var obj:Object = {};
					obj.energy = xxx.@energy;
					obj.hp = xxx.@hp;
			     	ObjDataManage.getInstance().plantInfo[xxx.@name] = obj;
				}
			}
			//解析游戏小提示文字
			if(xml.hasOwnProperty("tips")){
				xmlList = xml.tips.children();
				for each(xxx in xmlList){
					var str:String = xxx.@content;
					ObjDataManage.getInstance().tips.push(str);
				}
			}
			
			//解析路点
			if(xml.hasOwnProperty("line")){
				xmlList = xml.line.children();
				line.length = 0;
				for each(xxx in xmlList){
					point = new Point(xxx.@x,xxx.@y);
					line.push(point);
				}
			}
			//解析公共障碍点
			if(xml.hasOwnProperty("barrierCommon")){
				xmlList = xml.barrierCommon.children();
				commonBarrier.length = 0;
				for each(xxx in xmlList){
					point = new Point(xxx.@x,xxx.@y);
					commonBarrier.push(point);
				}
			}
			//解析障碍点
			if(xml.hasOwnProperty("barrier")){
				xmlList = xml.barrier.children();
				barrier.length = 0;
				for each(xxx in xmlList){
					point = new Point(xxx.@x,xxx.@y);
					barrier.push(point);
				}
			}
			//解析植物数据
			if(xml.hasOwnProperty("plants")){
				xmlList = xml.plants.children();
				ObjDataManage.getInstance().plantAry.length = 0;
				var plantProp:PlantProp;
				for each(xxx in xmlList){
					plantProp = new PlantProp();
					plantProp.x = xxx.@x;
					plantProp.y = xxx.@y;
					plantProp.blood = xxx.@hp;
					plantProp.energy = xxx.@energy;
					var str:String = xxx.@gradlist;
					var gradAry:Array = str.split(",");
					i=0;
					lens = gradAry.length;
					for(i ;i<lens;i++){
						var arr:Array = gradAry[i].split(":");
						point = new Point(arr[0],arr[1]);
						plantProp.gradAry.push(point);
					}
					plantProp.name = xxx.@cls;
					plantProp.wid = xxx.@width;
					plantProp.height = xxx.@height;
					ObjDataManage.getInstance().plantAry.push(plantProp);
				}
			
			}
			//解析monster
			if(xml.hasOwnProperty("monster")){
				xmlList = xml.monster.children();
				for each(xxx in xmlList){
					var prop:MonsterProp = new MonsterProp();
					prop.name = xxx.@name;
					prop.blood = xxx.@blood;
					prop.speed = xxx.@speed;
					prop.energy = xxx.@energy;
					prop.exp_rad = xxx.@explodRidus;
					prop.hurt = xxx.@hurt;
					ObjDataManage.getInstance().monsterMap[prop.name] = prop;
				}
			}
		
			//解析tank
			if(xml.hasOwnProperty("tank")){
				xmlList = xml.tank.children();
				for each(xxx in xmlList){
					var tp:TankProp = new TankProp();
					tp.name = xxx.@name;
					tp.bulletType = xxx.@bullet;
					tp.shoutGap = xxx.@shoutGap;
					tp.shoutRadius = xxx.@shoutRadius;
					tp.rotateSpeed = xxx.@rotateSpeed;
					tp.revive = xxx.@revive;
					tp.nameDes = xxx.@nameDes;
					tp.blood = xxx.@blood;
					ObjDataManage.getInstance().tankMap[tp.name] = tp;
				}
			}
			//解析飞机
			if(xml.hasOwnProperty("fly")){
				xmlList = xml.fly.children();
				for each(xxx in xmlList){
					var fp:FlyProp = new FlyProp();
					fp.name = xxx.@name;
					fp.nameDes = xxx.@nameDes;
					fp.bulletType = xxx.@bullet;
					fp.shoutGap = xxx.@shoutGap;
					fp.shoutRadius = xxx.@shoutRadius;
					fp.rotateSpeed = xxx.@rotateSpeed;
					fp.revive = xxx.@revive;
					fp.b_num = xxx.@b_num;
					fp.speed = xxx.@speed;
					fp.blood = xxx.@blood;
					fp.killSelfRat = xxx.@killSelfRat;
					ObjDataManage.getInstance().tankMap[fp.name] = fp;
				}
			}
		
			//解析bullet
			if(xml.hasOwnProperty("bullet")){
				xmlList = xml.bullet.children();
				for each(xxx in xmlList){
					var bp:BulletProp;
					if(xxx.@hurtRange!=undefined){
						bp = new EBulletProp();
						(bp as EBulletProp).hurtRange = xxx.@hurtRange;
					}else{
						bp = new BulletProp();
					}
					bp.explode = xxx.@explode;
					bp.name = xxx.@name;
					bp.art = xxx.@art;
					bp.speed = xxx.@speed;
					ObjDataManage.getInstance().bulletMap[bp.name] = bp;
				}
			}
			
			//解析monster队列
			if(xml.hasOwnProperty("wave")){
				xmlList = xml.wave.children();
				lens = xmlList.length();
				i=0;
				for(i;i<lens;i++){
					xxx = xmlList[i];
					var waveProp:WaveProp = new WaveProp();
					waveProp.gapTime = xxx.@gap;
					var ary1:Array = xxx.@list.split(",");
					while(ary1.length){
						var ary2:Array = ary1.shift().split("_");
						var monsterProp:MonsterProp = ObjDataManage.getInstance().getMonsterProp(ary2[0]);
						monsterProp.gapTime = int(ary2[1]);
						waveProp.list.push(monsterProp);
					}
					ObjDataManage.getInstance().monsterQueue.push(waveProp);
				}
				QueueManage.getInstance().queueNum = lens;
			}
			trace(request.url + "  complete");
			destroyEvent();
			if(loaderComplete != null)loaderComplete();
		}
		
		public function copeline(ary:Array):void{
			ary.length = 0;
			for(var i:int=0;i<line.length;i++){
				ary.push(line[i]);
			}
		}
		
	}
}